<%@ page import="java.util.*" %>

<%!
public HashMap getMarks(HttpServletRequest request) {
	HashMap retHash = new HashMap();
	Cookie[] cookies = request.getCookies(); 
	for (int c=0;c<cookies.length;c++) {	
		if("marks".equals(cookies[c].getName())) {
		    StringTokenizer st = new StringTokenizer(cookies[c].getValue(), ",");
		    while(st.hasMoreTokens())
			retHash.put(st.nextToken(),1);			
		}
	}
	return retHash;
}

public String join(Collection s, String delimiter) {
	StringBuffer buffer = new StringBuffer();
	Iterator iter = s.iterator();
	while (iter.hasNext()) {
	    buffer.append(iter.next());
	    if (iter.hasNext()) {
		buffer.append(delimiter);
	    }
	}
	return buffer.toString();
}


%>



<%

HashMap marks = null;
String action = request.getParameter("action");
if("add".equals(action)) {
	String addC = request.getParameter("addC");
	if(addC == null || addC.trim().equals("") || addC.trim().length() > 6)
		return;
	
	marks = getMarks(request);
	marks.put(addC, 1);	
} else if("del".equals(action)) {
	String delC = request.getParameter("delC");
	if(delC == null || delC.trim().equals("") || delC.trim().length() > 6)
		return;
	
	marks = getMarks(request);
	marks.remove(delC);	
}

if(marks != null && marks.size() >= 0) {
	Cookie outC = new Cookie ("marks",join(marks.keySet(), ","));
	outC.setMaxAge(365 * 24 * 60 * 60);
	response.addCookie(outC);
}








%>